/**
 * 门店房间选择功能集成测试
 * 
 * 用途：测试门店和房间选择功能的完整流程
 * 方法：模拟API调用和UI交互，验证数据流和状态管理
 * 原理：使用MockWebServer模拟API响应，测试各个组件的集成效果
 */
package com.example.brain_wave.test.store_room

import com.example.brain_wave.bluetooth.api.StoreRoomApiClient
import com.example.brain_wave.bluetooth.model.*
import com.google.gson.Gson
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.Assert.*
import android.content.Context
import androidx.test.core.app.ApplicationProvider

class StoreRoomIntegrationTest {
    
    private val gson = Gson()
    
    @Test
    fun testStoreDataModel() {
        // 测试门店数据模型
        val store = Store(
            id = 1,
            name = "测试门店",
            province_id_text = "测试省份"
        )
        
        assertEquals(1, store.id)
        assertEquals("测试门店", store.name)
        assertEquals("测试省份", store.province_id_text)
    }
    
    @Test
    fun testRoomDataModel() {
        // 测试房间数据模型
        val room = Room(
            id = 1,
            name = "测试房间",
            store_id = 1
        )
        
        assertEquals(1, room.id)
        assertEquals("测试房间", room.name)
        assertEquals(1, room.store_id)
    }
    
    @Test
    fun testStoreResponseParsing() {
        // 测试门店响应数据解析
        val jsonResponse = """
        {
            "code": 1,
            "msg": "success",
            "data": {
                "list": [
                    {
                        "id": 1,
                        "name": "门店1",
                        "province_id_text": ""
                    },
                    {
                        "id": 2,
                        "name": "门店2", 
                        "province_id_text": ""
                    }
                ]
            }
        }
        """.trimIndent()
        
        val storeResponse = gson.fromJson(jsonResponse, StoreResponse::class.java)
        
        assertEquals(1, storeResponse.code)
        assertEquals("success", storeResponse.msg)
        assertEquals(2, storeResponse.data.list.size)
        assertEquals("门店1", storeResponse.data.list[0].name)
        assertEquals("门店2", storeResponse.data.list[1].name)
    }
    
    @Test
    fun testRoomResponseParsing() {
        // 测试房间响应数据解析
        val jsonResponse = """
        {
            "code": 1,
            "msg": "success",
            "data": {
                "list": [
                    {
                        "id": 1,
                        "name": "房间1",
                        "store_id": 1
                    },
                    {
                        "id": 2,
                        "name": "房间2",
                        "store_id": 1
                    }
                ]
            }
        }
        """.trimIndent()
        
        val roomResponse = gson.fromJson(jsonResponse, RoomResponse::class.java)
        
        assertEquals(1, roomResponse.code)
        assertEquals("success", roomResponse.msg)
        assertEquals(2, roomResponse.data.list.size)
        assertEquals("房间1", roomResponse.data.list[0].name)
        assertEquals("房间2", roomResponse.data.list[1].name)
        assertEquals(1, roomResponse.data.list[0].store_id)
    }
    
    @Test
    fun testApiClientInitialization() {
        // 测试API客户端初始化
        val context = ApplicationProvider.getApplicationContext<Context>()
        val apiClient = StoreRoomApiClient(context)
        apiClient.setBaseUrl("http://test.example.com")
        
        var storeListReceived = false
        var roomListReceived = false
        var errorReceived = false
        
        apiClient.setOnStoreListReceived { stores ->
            storeListReceived = true
        }
        
        apiClient.setOnRoomListReceived { rooms ->
            roomListReceived = true
        }
        
        apiClient.setOnError { error ->
            errorReceived = true
        }
        
        // 验证回调设置成功（通过调用API方法来间接验证）
        assertNotNull(apiClient)
    }
    
    @Test
    fun testDataFlowIntegration() {
        // 测试数据流集成
        val stores = listOf(
            Store(1, "门店1", ""),
            Store(2, "门店2", "")
        )
        
        val rooms = listOf(
            Room(1, "房间1", 1),
            Room(2, "房间2", 1)
        )
        
        // 模拟选择门店
        val selectedStore = stores[0]
        assertEquals(1, selectedStore.id)
        
        // 模拟根据门店ID筛选房间
        val filteredRooms = rooms.filter { it.store_id == selectedStore.id }
        assertEquals(2, filteredRooms.size)
        
        // 模拟选择房间
        val selectedRoom = filteredRooms[0]
        assertEquals(1, selectedRoom.id)
        assertEquals("房间1", selectedRoom.name)
        
        // 验证房间ID转换
        val roomId = selectedRoom.id.toString()
        assertEquals("1", roomId)
    }
}